package icursoft.educationms.http;


import android.util.Log;

import org.greenrobot.eventbus.EventBus;

import icursoft.educationms.eventbus.EventError;
import icursoft.educationms.util.Const;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Administrator on 2016/12/22.
 */

public abstract class HttpCallBack<T extends HttpResult> implements Callback<T> {


    public abstract void onSuccess(T t);

    @Override
    public void onResponse(Call<T> call, Response<T> response) {
        if (response.code() == 200) {
            if (response.body().getMsg() >Const.HTTP_REQUEST_SUCCESS) {
                T t = (T) response.body();
                onSuccess(t);
            } else {
                EventBus.getDefault().post(new EventError(response.body().getMsg(),
                        response.body().getMsgbox()));
            }
        } else {
            EventBus.getDefault().post(new EventError(response.code(), null));
        }
    }

    @Override
    public void onFailure(Call<T> call, Throwable t) {
        Log.e("OkHttp", t.getMessage());
        EventBus.getDefault().post(new EventError(-1, null));
    }
}
